<script>
	$(function() {
		$("#uploadFiles").MultiFile({
			STRING: {
			file: '<em title="Click to remove" onclick="$(this).parent().prev().click()"><h3>$file</h3></em>',
			remove: '<img src="http://www.fyneworks.com/@/bin.gif" height="16" width="16" alt="X"/>'
			}
		});
		$('.deletable').click(function() {
			//var strNoidung = '';
			//$(this).parent('.noidung_item')
			$(this).parent().remove();
			//alert(strNoidung);
        });
		$("#cboCountry").change(function() { 
		/* do something here */ 
			var ajax_url = '../incs/list_school_by_country_id.php?qg_id='+ $("#cboCountry").val();
			$.get(ajax_url,function(data){
			$('#cboSchool').html(data);
			});
		});
		$("#cboSchool").change(function() { 
		/* do something here */ 
			var ajax_url = '../incs/list_dept_by_school_id.php?truong_id='+ $("#cboSchool").val();
			$.get(ajax_url,function(data){
			$('#cboDept').html(data);
			});
		});
		$("#txtNgaydang").datepicker({dateFormat: 'dd-mm-yy'});
		$("#cboDept").change(function() { 
		/* do something here */ 
			var ajax_url = '../tac_gia/list_author_by_dept_id.php?dv_id='+ $("#cboDept").val();
			$.get(ajax_url,function(data){
			$('#cboAuthor-Topic').html(data);
			});
		});
		
		$("#cboJournalStyle").change(function() { 
		/* do something here */ 
			var ajax_url = '../incs/list_journal_by_journalstyle_id.php?loaitc_id='+ $("#cboJournalStyle").val();
			$.get(ajax_url,function(data){
			$('#cboJournal').html(data);
			});
		});
		$("#cboJournal").change(function() { 
		/* do something here */ 
			var ajax_url = '../incs/list_number_by_journal_id.php?tc_id='+ $("#cboJournal").val();
			$.get(ajax_url,function(data){
			$('#cboNumber').html(data);
			});
		});
	});
</script>
<?php
if (!defined('WEB_ROOT')) {
	exit;
}
// make sure id exists
if (isset($_GET['id']) && $_GET['id'] >0) {
	$id = (int)$_GET['id'];
} else {
	// redirect to index.php 
	header('Location: index.php');
}
if (isset($_GET['khoi_id']) && $_GET['khoi_id'] > 0) {
	$khoi_id = (int)$_GET['khoi_id'];
} 
if (isset($_GET['tg_id']) && $_GET['tg_id'] > 0) {
	$tg_id = (int)$_GET['tg_id'];
}
// Lấy thông tin bài báo
$sql = "SELECT bb.id, mabb, tenbb, tomtat_bb, date_format(ngaydang,'%d/%m/%Y') as ngaydang1, khoi_id, loaitc_id, tentc, impact_factor, tenloai, so, ngayra, tc_id, tutrang, dentrang, noidung_bb
		FROM baibao bb join chitiet_tapchi cttc on bb.cttc_id = cttc.id join tapchi tc on cttc.tc_id = tc.id join loaitapchi ltc on tc.loaitc_id = ltc.id
		WHERE bb.id = $id and bb.hieu_luc = 1
		ORDER BY bb.id desc";
	
	//echo $sql; exit();
	
$result = mysql_query($sql) or die('Không lấy được thông tin bài báo. ' . mysql_error());
$row    = mysql_fetch_assoc($result);
extract($row);

//combo quốc gia
	$sql = "SELECT id, tenqg
				FROM quocgia
				ORDER BY id";
		$result = dbQuery($sql) or die('Không lấy được quốc gia. ' . mysql_error());
		
		$groups = array();
		$list4 = '';
		while($row = dbFetchArray($result)) {
			list($id_qg, $name) = $row;
			$list4 .= "<option value=\"$id_qg\"";
				if ($id_qg == $qg_id) {
					$list4.= " selected";
				}
				$list4 .= ">$name</option>\r\n";
		}	
		//combo trường
	$sql = "SELECT id, tendv
				FROM donvi where truong_id is NULL
				ORDER BY id";
		$result = dbQuery($sql) or die('Không lấy được trường. ' . mysql_error());
		
		$groups = array();
		$list3 = '';
		while($row = dbFetchArray($result)) {
			list($id_truong, $name) = $row;
			$list3 .= "<option value=\"$id_truong\"";
				if ($id_truong == $dv2_id) {
					$list3.= " selected";
				}
				$list3 .= ">$name</option>\r\n";
		}	

		//combo đơn vị
	$sql = "SELECT id, tendv
				FROM donvi
				ORDER BY id";
		$result = dbQuery($sql) or die('Không lấy được đơn vị. ' . mysql_error());
		
		$groups = array();
		$listD = '';
		while($row = dbFetchArray($result)) {
			list($id_dv, $name) = $row;
			$listD .= "<option value=\"$id_dv\"";
				if ($id_dv == $dv_id) {
					$listD.= " selected";
				}
				$listD .= ">$name</option>\r\n";
		}
// lấy thông tin khối
$sql = "SELECT id, tenkhoi
				FROM khoi
				ORDER BY id";
		$result = dbQuery($sql) or die('. ' . mysql_error());
		
		$groups = array();
		$list = '';
		while($row1 = dbFetchArray($result)) {
			list($id_khoi, $name) = $row1;
			$list .= "<option value=\"$id_khoi\"";
				if ($id_khoi == $khoi_id) {
					$list.= " selected";
				}
				$list .= ">$name</option>\r\n";
		}

// combo loại tạp chí
$sql = "SELECT id, tenloai
				FROM loaitapchi
				ORDER BY id";
		$result = dbQuery($sql) or die('. ' . mysql_error());
		
		$groups = array();
		$list1 = '';
		while($row1 = dbFetchArray($result)) {
			list($id_loaitc, $name) = $row1;
			$list1 .= "<option value=\"$id_loaitc\"";
				if ($id_loaitc == $loaitc_id) {
					$list1.= " selected";
				}
				if (substr($name, 0, 1) != "K") {
				$name = "Tạp chí ".$name;
			}
			$list1 .= ">$name</option>\r\n";
		}
		//combo tạp chí
		$sql = "SELECT id, tentc
				FROM tapchi WHERE loaitc_id = $loaitc_id
				ORDER BY id";
		$result = dbQuery($sql) or die('. ' . mysql_error());
		
		$groups = array();
		$list2 = '';
		while($row1 = dbFetchArray($result)) {
			list($id_tc, $name) = $row1;
			$list2 .= "<option value=\"$id_tc\"";
				if ($id_tc == $tc_id) {
					$list2.= " selected";
				}
				$list2 .= ">$name</option>\r\n";
		}
		//combo số ra, ngày ra
		$sql = "SELECT id, concat(cast(so AS char(3)),'----',cast(date_format(ngayra ,'%d-%m-%Y')as char(10))) AS ngayra
			FROM chitiet_tapchi cttc
			WHERE cttc.tc_id = $tc_id AND hieu_luc =1
			ORDER BY ngayra asc";
		$result = dbQuery($sql) or die('. ' . mysql_error());
		
		$groups = array();
		$numberList = '';
		while($row1 = dbFetchArray($result)) {
			list($id_cttc, $name) = $row1;
			$numberList .= "<option value=\"$id_cttc\"";
				if ($id_cttc == $cttc_id) {
					$numberList.= " selected";
				}
				$numberList .= ">$name</option>\r\n";
		}
?> 
<div class="onecolumn">
				<div class="header">
					<span>Sửa bài báo</span>
				</div>
				<br class="clear"/>
				<div class="content">
					<form action="processPaper.php?action=modifyPaper&id=<?php echo $id; ?>" method="post" enctype="multipart/form-data" name="frmAddPaper" id="frmAddPaper">
						  <input name="tg" id="tg" type="hidden" />
						  <input name="tg_sel" id="tg_sel" type="hidden" />
						  <input name="noidung_bb" id="noidung_bb" type="hidden" />
					 
					 <table width="100%" border="0" align="center" cellpadding="5" cellspacing="1" class="entryTable">
					  <tr> 
						   <td width="150" class="label">Mã bài báo</td>
						   <td colspan="5" class="content"> <input name="txtMabb" type="text" class="box" id="txtMabb" readonly="true" style="background-color:#DADADA" value="<?php echo $mabb; ?>"  size="20" maxlength="20"></td>
					  </tr>
					  <tr> 
						   <td width="150" class="label">Tiêu đề bài báo</td>
						<td colspan="5" class="content"> <input name="txtTieude" type="text" class="box" id="txtTieude" size="118" maxlength="200" value = <?php echo $tenbb; ?> /> </td>  </tr>
					  <tr> 
						   <td width="150" class="label">Tóm tắt bài báo</td>
						   <td colspan="5" class="content"> <textarea name="mxtTomtat" cols="120" rows="8" class="box" id="mxtTomtat"><?php echo $tomtat_bb; ?></textarea> </td>
					  </tr>
					   <tr>
							<td class="label">Nội dung bài báo</td>
							<td class="content">
								<input id="uploadFiles" name="uploadFiles[]" type="file" class="multi" maxlength="10"/>
									<?php 
										//echo $noidung_bb;
										$arr = explode(",", $noidung_bb);
										//$ngayketthuc = $arr[2] . "-" . $arr[1] . "-" . $arr[0];
										echo "<div id='noidung_list' >";
										for ($i = 0; $i < count($arr); $i++){
											echo "<div id='nddt_$i'><a href='javascript:void(0);' class='deletable'>x</a> <a href='../../data/".$arr[$i]."' class='noidung_item' >".$arr[$i]."</a></div>";
										}
										echo "</div>"
									?>
							</td>
					  </tr>
						<tr> 
					   <td width="150" class="label">Khối ngành</td>
					   <td colspan="5" class="content"> <select name="cboStyle" id="cboStyle" class="box">
						 <option value="" selected>-- Khối --</option>
							<?php
								echo $list;
							?>	 
						</select></td>
					  </tr>
					  <tr> 
						   <td width="150" class="label">Ngày đăng</td>
						   <td width="150" class="content" colspan="5">
						   		<input name="txtNgaydang" type="text" id="txtNgaydang" size="17" maxlength="20" class="box" value="<?php echo $ngaydang1; ?>" > 
								&nbsp;&nbsp;Từ trang&nbsp;&nbsp;
								<input  name="txtTutrang" type="text" id = ="txtTutrang" value="<?php echo $tutrang; ?>" size="17" maxlength="20" /> 
								&nbsp;&nbsp;Đến trang&nbsp;&nbsp; <input name="txtDentrang" type="text" id = ="txtDentrang" value="<?php echo $dentrang; ?>" size="20" maxlength="20" />
							</td>
					  </tr>
					  <tr> 
						   <td width="150" class="label">Loại tạp chí (*)</td>
						   <td class="content" colspan="5"> <select name="cboJournalStyle" id="cboJournalStyle" class="box">
								<option value="" selected>-- Loại tạp chí --</option>
									<?php
										echo $list1;
									?>	 
								</select>&nbsp;&nbsp;Tạp chí&nbsp;&nbsp;
						  		<select name="cboJournal" id="cboJournal" class="box">
								<option value="" selected>-- Tạp chí --</option>
									<?php
										echo $list2;
									?>	 
								</select>		</td>
					  </tr>
					   <tr> 
						   <td width="150" class="label">Số / Ngày ra</td>
						   <td class="content"> <select name="cboNumber" id="cboNumber" class="box">
								<option value="0" selected>-- Chọn số ra --</option>
									<?php
										echo $numberList;
									?>	 
								</select>		</td>
						 
					   </tr>
					  <tr>
		   		<td colspan="6">
					 <div class="header"><span>Tác giả bài báo</span>
				</td>
		   </tr>
		   <tr>
		   		<td colspan="6">
					 <span class="luuy">Lưu ý: Chọn quốc gia/ trường/ đơn vị/ tác giả thực hiện bài báo rồi nhấn thêm người thực hiện để thêm tác giả bài báo hoặc nhập mới/ nhập thêm vào textbox nếu tác giả chưa có thông tin trong hệ thống.</span>
				</td>
		   </tr>
		   <tr>
			   <td width="150" class="label">Quốc gia</td>
					<td class="content"> <select name="cboCountry" id="cboCountry" class="box">
						 <option value="0" selected>-- Chọn quốc gia --</option>
								<?php
									echo $list4;
								?>	 
						</select></td>
					 <td width="150" class="label">Trường</td>
					 <td class="content" colspan="3"> <select name="cboSchool" id="cboSchool" class="box">
					   <option value="" selected>-- Chọn trường --</option>
							<?php
								echo $list3;
							?>	 
					   </select>
				 </td>
			</tr>
			<tr>
			   <td width="150" class="label">Đơn vị</td>
			   <td class="content"> <select name="cboDept" id="cboDept" class="box">
					   <option value="" selected>-- Chọn đơn vị --</option>
							<?php
								echo $listD;
							?>	 
					   </select>
				 </td>
				<td width="150" class="label">Tác giả</td>
				<td align="left" class="content" colspan="5"> <select name="cboAuthor-Topic" id="cboAuthor-Topic" class="box">
						<option value="0" selected>-- Chọn tên người thực hiện --</option>
						</select><input  align="left" size="20" name="btnAddPerson" type="button" id="btnAddPeson" 
					   value="Thêm tác giả bài báo" onclick="appendRow()"class="box"> </td>
			</tr>
		 </table>
				 <table id="tbl_nth" width="100%" id="t1" border="1" align="center" cellpadding="2" cellspacing="1"  class="data" >
					  <!--<tr align="center" id="listTableHeader">-->
					 <tr> 
						  <th width="0%"  style="display:none"></th>
						  <th width="0%"  style="display:none"></th>
						  <th width="0%"  style="display:none"></th>
						  <th width="30%">Họ tên tác giả</th>
						  <th width="30%">Đơn vị</th>
						  <th width="30%">Trường</th>
						  <th width="10%">Loại bỏ</th>
						  <th width="0%" style="display:none"></th>
   						  <th width="0%" style="display:none"></th>
					</tr>	
					<?php 
						$sql_nth = "SELECT tg.id AS id1, dv1.id AS id2, dv2.id as id3, hotentg, dv1.tendv, dv2.tendv as tentruong
									FROM tacgia tg
									JOIN tacgia_baibao tgb ON tg.id = tgb.tg_id
									JOIN (donvi dv1 join donvi dv2 on dv1.truong_id = dv2.id) ON tg.dv_id = dv1.id
									WHERE tgb.bb_id = $id";
						
						$result2 = dbQuery($sql_nth) or die('' . mysql_error());
					
						if (mysql_num_rows($result2)<>0){
							$i = 0;
							while ($row = mysql_fetch_array($result2)){
								$i= $i + 1;
								$id1 = $row["id1"];
								$id2 = $row["id2"];
								$hotentg = $row["hotentg"];
								$tendv = $row["tendv"];
								$tentruong = $row["tentruong"];
								echo "<tr>"; 
								  echo "<td width='0%' style='display:none'>$id1</td>";
								  echo "<td width='0%' style='display:none'>$id2</td>";
								  echo "<td width='0%' style='display:none'>$id3</td>";
								  echo "<td width='25%'>$hotentg</td>";
								  echo "<td width='30%'>$tendv </td>";
								  echo "<td width='30%'>$tentruong </td>";
								  echo "<td width='15'><a href='javascript:deleteRow($i);'>X</a></td>"; 
								  echo "<td width='0%' style='display:none'>$i</td>";
								  echo "<td width='0%' style='display:none'></td>";
								echo "</tr>";
							}
							echo "<script> $(function() {gCount = ($i+1);}); </script>";
					}
					?>
				
				  </table>
				  <br />
				  <br />
		   <p align="center"> 
		  <input name="btnModifyPaper" type="button" id="btnModifyPaper" value="Lưu thay đổi" onClick="checkAddPaperForm(false);" class="box">
		  &nbsp;&nbsp;<input name="btnCancel" type="button" id="btnCancel" value=" Hủy " onClick="window.location.href='index.php';" class="box">  
		 </p>
		</form>
	</div>
</div>
